import { Component, Input, OnInit } from '@angular/core';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
@Component({
  selector: 'app-app-combo-list',
  templateUrl: './app-combo-list.component.html',
  styleUrls: ['./app-combo-list.component.scss'],
})
export class AppComboListComponent implements OnInit {
  @Input() tagData: any;
  pageNo = 1;
  pageSize: number;
  pageTotal: number;
  tempEvent: any;
  params: any = { content: {} };
  sessionId: string;
  listData: any;
  showLoadMore = false;
  constructor(
    public pageModalService: PageModalService,
    private httpService: HttpService,
    private appGlobalService: AppGlobalService,
  ) { }

  ngOnInit() {
      this.sessionId = localStorage.getItem('sessionId');
      if (this.tagData.data.submit !== undefined) {
        // 初始化参数
        this.params = this.tagData.data.submit;
        this.params.sessionId = this.sessionId;
        this.listData = this.tagData.data.data;
        this.pageSize = this.tagData.pageSize;
        this.pageTotal = this.tagData.pageTotal;

        this.showLoadMore = !this.tagData.data.moreButton &&
          this.tagData.data.submit &&
          this.pageTotal &&
          (this.pageNo < Math.ceil(this.pageTotal / this.pageSize)) &&
          this.tagData.asyncShow !== 'true';
      }
  }

  openPage(link) {
    this.pageModalService.openPage(link);
  }

  loadMoreData(event) {
    try {
      this.tempEvent = event;
      // // 请求下一个页面
      this.pageNo = this.pageNo + 1;
      // // 参数
      this.params.content.pageNo = this.pageNo;
      // 请求获取新数据
      this.httpService.post('application/submit', this.params, res => {

        // 请求数据成功
        if (res.success) {
          const resList = res.data.data;
          // 追加返回数据
          if (resList && resList.length > 0) {
            // 追加数据
            resList.map(v => {
              this.listData.push(v);
            });
          } else {
            event.target.disabled = true;
          }
        } else {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, 'middle');
        }
        event.target.complete();

      }, err => {
        // 请求失败，更新当前页数
        this.pageNo = this.pageNo - 1;
        // 请求数据失败
        this.appGlobalService.showToast(err.message, 2000, 'middle');
        event.target.complete();

      });
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
      event.target.complete();

    }
  }
}
